home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / collectn / mortgage.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  2.1 KB  |  65 lines  |  [TEXT/ttxt]

  1. --<<<
  2.  
  3. class MortgagePayments (IndirectCollection)
  4.     instance variables
  5.         totalPayments -- a virtual instance variable
  6.         presentValue -- a virtual instance variable
  7.     instance methods
  8.         method init self #rest args -> (
  9.             apply nextMethod self targetCollection:(new SortedArray) args
  10.         )
  11.         method isAppropriateObject self addedObject -> (
  12.             -- check that it is the right kind of object
  13.             if not isAKindOf addedObject Pair do (
  14.                 format debug "not a pair\n" undefined @normal
  15.                 return false
  16.             )
  17.             if not isAKindOf addedObject[1] Date do (
  18.                 format debug "first element not a date\n" undefined @normal
  19.                 return false
  20.             )
  21.             if not isAKindOf addedObject[2] Number do (
  22.                 format debug "second element not a number\n" undefined @normal
  23.                 return false
  24.             )
  25.             return true
  26.         )
  27.         method objectAdded self addedKey addedObject -> (
  28.             -- does nothing for now
  29.             return true
  30.         )
  31.         method objectRemoved self objectRemoved -> (
  32.             -- does nothing for now
  33.             return true
  34.         )
  35. end -- MortgagePayments
  36.  
  37. method totalPaymentsGetter self {class MortgagePayments} -> (
  38.     local sum := 0
  39.     forEach self (x -> sum := sum + x[2]) undefined
  40.     return sum
  41. )
  42. method getNetPresentValue self {class MortgagePayments} discountRate -> (
  43.     local sum := 0
  44.     local today := theCalendarClock.date
  45.     forEach self (x -> sum := sum + presentValue x today discountRate) undefined
  46.     return sum
  47. )
  48.  
  49. function presentValue payment today  discountRate -> (
  50.     local conversion := (60 * 60 * 24 * 365.25)
  51.     local y := (today as LargeInteger) / conversion
  52.     local z := (payment[1] as LargeInteger) / conversion
  53.     return (payment[2] * exp (negate(discountRate * (z - y))))
  54. )
  55.  
  56.  
  57. -- create an instance, and then add some data to it
  58. myPayments := new MortgagePayments
  59. add myPayments empty (new Pair values:#((new Date year:1996 month:@july),50000))
  60. add myPayments empty (new Pair values:#((new Date year:1997 month:@july),50000))
  61. add myPayments empty (new Pair values:#((new Date year:1998 month:@july),50000))
  62. add myPayments empty (new Pair values:#((new Date year:1999 month:@july),50000))
  63. add myPayments empty (new Pair values:#((new Date year:2000 month:@july),50000))
  64.  
  65. -->>>